$(function(){
    //获取手机验证码
    $('.yzmBtn').click(function(){
        var myreg = /^[1][0-9][0-9]{9}$/;
        var mobile = $('.telephone').val();
        var imgcode = $(".imgcode").val();
        if( mobile == "" || !myreg.test(mobile) || mobile.length != 11){
            layerBox("请输入正确的手机号码")
            return false;
        }else if(imgcode == ""){
            layerBox("请输入图片验证码")
            return false;
        } else {
            Kyz($(".yzmBtn"),"重新发送",60);
            var index = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            $.ajax({
                url:"/user/sendsms",
                type:"POST",
                data:{_token:token,phone:mobile,type:2,imgcode:imgcode},
                success:function(msg){
                    layer.close(index);
                    var obj = eval("("+msg+")");
                    if(obj['code'] == 1)
                    {
                        layerBox(obj['msg']);s
                    }else if(obj['code'] == -1){
                        layerBox(obj['msg']);
                    }
                },
                error:function(){
                    layer.close(index);
                    layerBox("服务器繁忙,请稍后重试!");
                }
            });
        }
    });
    $(".sub_btn").click(function(){
        var telephone = $(".telephone").val();
        var imgcode = $(".imgcode").val();
        var yzcode = $(".yzcode").val();
        var password = $(".password").val();
        var newcode = $(".newcode").val();
        var telExp = /^[1][0-9][0-9]{9}$/;
        if(telephone == '' || telephone.length != 11 || !telExp.test(telephone)){
            layerBox("请输入正确的手机号码");
            return false;
        }else if(imgcode == ''){
            layerBox("请输入正确的图像验证码");
            return false;
        }else if(yzcode == ''){
            layerBox("请输入正确的验证码");
            return false;
        }else if(password == ''){
            layerBox("请输入您的新密码");
            return false;
        }else if(newcode == ''){
            layerBox("请再次输入您的新密码");
            return false;
        }else if(newcode != password){
            layerBox("密码不一致");
            return false;
        }else{
            var index = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            $.ajax({
                url:"/user/replaypass",
                type:"POST",
                data:{_token:token,phone:telephone,imgcode:imgcode,yzcode:yzcode,password:password,newcode:newcode},
                success:function(msg){
                    layer.close(index);
                    var obj = eval("("+msg+")");
                    if(obj['code'] == 1)
                    {
                        layerBox(obj['msg']);s
                    }else if(obj['code'] == -1){
                        layerBox(obj['msg']);
                    }
                },
                error:function(){
                    layer.close(index);
                    layerBox("服务器繁忙,请稍后重试!");
                }
            });
        }
    })
})
//验证码倒计时
function Kyz(kbt,msg2,ktime){
    kbt.attr('disabled','disabled');
    var t=setInterval(function(){
        ktime--;
        if(ktime==0){
            kbt.html(msg2);
            //这里的清除是起到了作用的。如果要在外部清除，就需要返回值。
            kbt.removeAttr('disabled');
            clearInterval(t);
        }else{
            kbt.html(ktime+"秒");
        };
    },1000);
    return t;
};
//错误提示
function layerBox(msg){
    layer.msg(msg);
}